// formPackage/pages/platformModule/bindingHouse/bindingHouse.js
import { publishPlatformHouse } from "../../../../service/platform_service"
import { queryNewHouseByCondition, searchHouseByWords } from "../../../../service/house_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    formData: {
      houseName: "",
      houseAddress: "",
      areaName: "",//区域
      areaCode: "",
      latitude: "",
      longitude: "",
      provinceName: "",
      provinceCode: "",
      cityName: "",
      cityCode: "",
      unitPrice: "",
      totalPrice: "",
      apartmentUnitPrice: "",
      apartmentTotalPrice: "",
      businessUnitPrice: "",
      businessTotalPrice: "",
      merchantsUnitPrice: "",
      merchantsTotalPrice: "",
      officeBuildingUnitPrice: "",
      officeBuildingTotalPrice: "",
      formatArea: "",
      isRent: false,
      joinTag: "住宅",
    },
    requestData: {
      cityName: "",
      houseStyle: "",
      maxArea: null,
      maxTotalPrice: null,
      maxUnitPrice: null,
      minArea: null,
      minTotalPrice: null,
      minUnitPrice: null,
      page: 0,
      pageSize: 5,
      saleStatus: "",
      subway: "",
      tags: [],
      words: "",
      zone: ""
    },
    showCityArea: false,
    searchHouseList: [

    ],
    showRemindPop: false,//提示弹窗
    proxyCommissionTypes: [
      {
        name: "住宅",
        isChoose: true,
      },
      {
        name: "公寓",
        isChoose: false,
      },
      {
        name: "商铺",
        isChoose: false,
      },
      {
        name: "招商",
        isChoose: false,
      },
      {
        name: "写字楼",
        isChoose: false,
      },

    ],
    isBusiness: false,
    isApartment: false,
    isHouse: true,//默认住宅
    isRent: false,//招商
    isOffice: false,//写字楼

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //输入绑定
  inputCellChange(e) {
    let item = e.detail;
    let changeValue = `formData.${item.key}`
    this.setData({
      [changeValue]: item.value
    })
    //如果是房源名需要搜索
    if (item.key == "houseName") {
      this.setData({
        'requestData.words': item.value,
      })

      debounce(this.searchHouseByWords);

    }
  },

  //代理类型选择:
  proxyCommissionAction(e) {
    let index = e.currentTarget.dataset.index;
    let isChoose = e.currentTarget.dataset.item.isChoose;
    let item = 'proxyCommissionTypes[' + index + '].isChoose';
    this.setData({
      [item]: !isChoose,
    })
    let arr = this.data.proxyCommissionTypes;
    let textArr = [];
    let isApartment = false;
    let isBusiness = false;
    let isHouse = false;
    let isRent = false;
    let isOffice = false;
    arr.forEach((i) => {
      if (i.isChoose) {
        textArr.push(i.name);
        if (i.name == "公寓") {
          isApartment = true
        }
        if (i.name == "商铺") {
          isBusiness = true;
        }
        if (i.name == "住宅") {
          isHouse = true
        }
        if (i.name == "招商") {
          isRent = true
        }
        if (i.name == "写字楼") {
          isOffice = true
        }
      }
    })

    this.setData({
      'formData.joinTag': textArr.join(","),
      isBusiness: isBusiness,
      isApartment: isApartment,
      isHouse: isHouse,
      isRent: isRent,
      isOffice: isOffice
    })
  },
  //是否带租约选择
  changeRentAction(e) {
    let text = e.currentTarget.dataset.text;
    this.setData({
      'formData.isRent': text,
    })
  },
  //搜索房源:
  searchHouseByWords() {
    wx.showLoading();
    searchHouseByWords(1, this.data.requestData.words).then(res => {
      wx.hideLoading();
      console.log("JJLog:搜索结果:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data instanceof Array) {
          this.setData({
            searchHouseList: res.data || []
          })
        }

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "搜索失败",
        icon: "none"
      })
      console.error("JJLog:searchHouseByWords", err);
    })
  },
  //选择城市区域等:
  selectAction(e) {
    let key = e.detail.key;
    if (key == "areaName") {
      this.setData({
        showCityArea: true
      })
    } else if (key == "houseAddress") {
      this.getSellLocationPush(key);
    }
  },
  //获取详细地址
  getSellLocationPush(type) {

  },




  cityPopConfirm(e) {
    let result = e.detail.result;//区县一层的
    this.setData({
      'formData.areaName': result[2].name,
      'formData.areaCode': result[2].code,
      'formData.cityName': result[1].name,
      'formData.cityCode': result[1].code,
      'formData.provinceName': result[0].name,
      'formData.provinceCode': result[0].code,
      showCityArea: false,
    })
  },
  cityPopHidden() {
    this.setData({
      showCityArea: false
    })
  },



  //确定发布房源:
  submitHouseAction() {
    let data = this.data.formData;
    if (!data.houseName) {
      wx.showToast({
        title: '请填写楼盘名称',
        icon: "none"
      })
      return;
    }
    if (!data.areaName) {
      wx.showToast({
        title: '请选择楼盘所在区域',
        icon: "none"
      })
      return;
    }
    if (!data.houseAddress) {
      wx.showToast({
        title: '请选择楼盘具体地址',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交参数是:" + JSON.stringify(data));

    debounce(this.publishPlatformHouse);


  },

  //发布房源请求:
  publishPlatformHouse() {
    wx.showLoading();
    publishPlatformHouse(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:发布结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          showRemindPop: true,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:publishPlatformHouse", err);
    })
  },




  //确定点击提示弹窗:
  sureRemindPopAction() {
    this.setData({
      showRemindPop: false
    })
    wx.navigateBack({
      delta: 0,
    })
  },

  //点击搜索房源的跳转:
  searchHouseTapAction(e) {
    let houseNumber = e.currentTarget.dataset.number;
    //跳转到详情且判断是否是绑定房源
    wx.navigateTo({
      url: '/newHousePackage/pages/newHouseDetail/newHouseDetail?houseNumber=' + houseNumber + "&bindHouse=" + 1,
    })
  },









})